<template>
  <a-select
    style="width:100%;"
    showSearch
    allowClear
    :disabled="disabled"
    :filterOption="filterOption"
    :placeholder="placeholder"
    @change="handleChange"
    :value="seletedValue"
  >
    <a-select-option
      v-for="(item) in orgs"
      :key="item.pid">
      {{ item.orgName }}
    </a-select-option>
  </a-select>
</template>

<script>
import { getOrgList } from '../org/org/org'
import storage from 'store'

export default {
  name: 'OrgSelect',
  props: {
    placeholder: {
      default: '请选择单位',
      required: false,
      type: String
    },
    disabled: {
      default: false,
      required: false,
      type: Boolean
    },
    value: {
      default: '',
      required: false,
      type: String
    }
  },
  data () {
    return {
      org: storage.get('org'),
      orgs: [],
      seletedValue: this.value
    }
  },
  created () {
    this.initDictData()
  },
  methods: {
    initDictData () {
      const parameter = {
        pageSize: 1000000
      }
      const headers = {
        authOption: '1'
      }
      getOrgList(parameter, headers).then(r => {
        if (r && r && r.code === 'C1' && r.data) {
          this.orgs = r.data
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange (val) {
      console.log(val)
      this.seletedValue = val
      this.$emit('change', val)
      // this.$emit('dataChanged', val)
    }
  },
  watch: {
    value: function () {
      this.seletedValue = this.value
    }
  }
}
</script>
